package com.mongodb.mongodbboot.controller;

import com.mongodb.mongodbboot.dao.ProductDao;
import com.mongodb.mongodbboot.dao.UserDao;
import com.mongodb.mongodbboot.model.ProductDO;
import com.mongodb.mongodbboot.model.Profile;
import com.mongodb.mongodbboot.model.UserDO;
import com.mongodb.mongodbboot.model.UserDONoID;
import com.mongodb.mongodbboot.service.SampleStatusService;
import com.mongodb.mongodbboot.service.UserNoIdRepository;
import com.mongodb.mongodbboot.service.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @author ：hyq
 * @date ：Created in 2020/8/12 15:10
 * @description：
 * @modified By：
 */
@RestController
public class MongodbCRUD {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private UserNoIdRepository userNoIdRepository;

    @Autowired
    private UserDao userDao;
    @Autowired
    private ProductDao productDao;
    @Autowired
    SampleStatusService sampleStatusService;

    @GetMapping("gettest")
    public void getTest(){
        sampleStatusService.sampleStatus();
    }

    /**
     * 测试mongodb ObjectId
     * 同时意味着ObjectId大致会按照插入顺序进行排序，这对于某些方面起到很大作用
     */
    @GetMapping("insertincr")
    public void testInsert2() {
        // 创建 UserDO 对象
        UserDONoID user = new UserDONoID();
        user.setUsername("huynqiang");
        user.setPassword("123456");
        user.setCreateTime(new Date());
        // 创建 Profile 对象
        Profile profile = new Profile();
        profile.setNickname("胡强");
        profile.setGender(1);
        user.setProfile(profile);
        // 存储到 DB
        userNoIdRepository.insert(user);
    }

    @GetMapping("insert")
    public void testInsert() {
        // 创建 UserDO 对象
        UserDO user = new UserDO();
        user.setUsername("huynqiang");
        user.setPassword("123456");
        user.setId(1);
        user.setCreateTime(new Date());
        // 创建 Profile 对象
        Profile profile = new Profile();
        profile.setNickname("胡强");
        profile.setGender(1);
        user.setProfile(profile);
        // 存储到 DB
        userRepository.insert(user);
    }
    @GetMapping("get")
    public Optional<UserDO> testSelectById() {

        Optional<UserDO> userDO = userRepository.findById(1);
        return  userDO;
    }

    @GetMapping("templateget")
    public UserDO TemplateSelectById() {
        UserDO userDO = userDao.findByUsername("胡云强");
        return  userDO;
    }
   /**
    * @Description: 嵌套查询
    * @Author: huyunqiang
    * @Date: 2020/8/12 15:44
    */
    @GetMapping("nickName")
    public List<UserDO> findAllByNickName(){
        List<UserDO> nickName = userDao.findAllByNickName("胡呆强");
        return  nickName;
    }

    /**
     * @Description: 测试自定义自增主键插入
     * @Author: huyunqiang
     * @Date: 2020/8/12 16:16
     */
    @GetMapping("incId")
    public void Insert() {
        // 创建 ProductDO 对象
        ProductDO product = new ProductDO();
        product.setName("你好啊");
        // 插入
        productDao.insert(product);
        // 打印 ID
        System.out.println(product.getId());
    }
    @GetMapping("test")
    public String test(String s){
        return s;
    }


}
